
#include "stdafx.h"
#include "Win32.Data.OleDb.h"
#include "Win32.String.h"

extern void TestOledbOracleClient();

namespace OleDb = Kif::Win32::Data::OleDbClient;
namespace KifWin32 = Kif::Win32;

#define SizeOfInBytes(addr) sizeof(addr)

// -----------------------------------------------------------------------------

/*
 * -- Test procedure
 * CREATE PROCEDURE TestSysUserByDeptId
 *    @DeptId CHAR(3)
 * AS
 * BEGIN
 *    select userid,lastname from sysuser where deptid=@DeptId
 * END
 * GO
 * -- select * from sysuser
 * -- exec TestSysUserByDeptId '006'
 */

// DID YOU CALLED "CoInitialize(NULL);" before this function? if not don't
// expect it to work.  Also remember to call "CoUninitialize();" aftewards...

void TestOleDbStoredProc1(
   OleDb::DbSettings &dbSettings)
{
   OleDb::DbConnection dbConn ;

   if (!dbConn.Connect(
      dbSettings.pHost, dbSettings.pCatalog, dbSettings.pUserId,
      dbSettings.pPassword))
      return;

   TCHAR *pQry = TEXT("{ ? = CALL TestSysUserByDeptId(?) }");

   OleDb::DbFields *pFields = dbConn.AllocFields(2);
   OleDb::DbParams *pParams = dbConn.AllocParams(2);

   if ((!pFields) && (!pParams))
   {
      // clean-up as needed
      if (pFields != NULL)
         delete pFields;
      if (pParams != NULL)
         delete pParams;
      dbConn.Disconnect();
      return;
   }

   const int UserIdLen   = 4;
   const int LastNameLen = 15;
   const int DeptLen     = 3;

   TCHAR UserId[UserIdLen+1];
   TCHAR LastName[LastNameLen+1];
   TCHAR Dept[DeptLen+1];

   *UserId = NULL;
   *LastName = NULL;

   pFields->Set(0,UserId,UserIdLen);
   pFields->Set(1,LastName,LastNameLen);

   long returnedValue;
   _tcscpy_s(Dept,TEXT("006")) ;
   pParams->Set(0,&returnedValue);
   pParams->Set(1,Dept,DeptLen);
   pParams->IsForOutput(0);

   OleDb::DbResult *pRes = dbConn.Prepare(pQry,pFields,pParams);
   if (pRes)
   {
      if (pRes->Execute())
      {
         if (pRes->GetResult())
         {
            pRes->Open();
            if (pRes->Next(pFields))
            {
               TCHAR format[] = _T("%s - %s\n");
               while (!pRes->Eof())
               {
                  _tprintf_s(format,UserId,LastName);
                  pRes->Next(pFields);
               }
            }
         }
      }
      pRes->Close();
   }

   // make sure to release allocate resources, else you will have
   // memory leaks!

   delete pRes;
   delete pParams;
   delete pFields;

   dbConn.Disconnect();
}  // end of TestOleDbStoredProc1

// -----------------------------------------------------------------------------

// DID YOU CALLED "CoInitialize(NULL);" before this function? if not don't
// expect it to work.  Also remember to call "CoUninitialize();" aftewards...

void TestOleDbSelect1(OleDb::DbSettings &dbSettings)
{
   OleDb::DbConnection dbConn;

   if (!dbConn.Connect(
      dbSettings.pHost, dbSettings.pCatalog, dbSettings.pUserId,
      dbSettings.pPassword))
      return;

   TCHAR *pQry = TEXT("select userid,lastname from sysuser where deptid=?");

   OleDb::DbFields *pFields = dbConn.AllocFields(2);
   OleDb::DbParams *pParams = dbConn.AllocParams(1);

   if ((!pFields) && (!pParams))
   {
      // clean-up as needed
      if (pFields != NULL)
         delete pFields;
      if (pParams != NULL)
         delete pParams;
      dbConn.Disconnect();
      return;
   }

   const int UserIdLen   = 4;
   const int LastNameLen = 15;
   const int DeptLen     = 3;

   TCHAR UserId[UserIdLen+1];
   TCHAR LastName[LastNameLen+1];
   TCHAR Dept[DeptLen+1];

   *UserId = NULL;
   *LastName = NULL;

   pFields->Set(0,UserId,UserIdLen);
   pFields->Set(1,LastName,LastNameLen);

   _tcscpy_s(Dept,TEXT("006"));
   pParams->Set(0,Dept,DeptLen);

   OleDb::DbResult *pRes = dbConn.Prepare(pQry,pFields,pParams);
   if (pRes)
   {
      if (pRes->Execute())
      {
         if (pRes->GetResult())
         {
            pRes->Open() ;
            if (pRes->Next(pFields))
            {
               while (!pRes->Eof())
               {
                  TCHAR format[] = _T("%s - %s\n");
                  _tprintf_s(format,UserId,LastName);
                  pRes->Next(pFields) ;
               }
            }
         }
      }
      pRes->Close() ;
   }

   // make sure to release allocate resources, else you will have
   // memory leaks!

   delete pRes;
   delete pParams;
   delete pFields;

   dbConn.Disconnect();
}  // end of TestOleDbSelect1

// -----------------------------------------------------------------------------

void TestOleDbClient()
{
   CoInitialize(NULL);

   try
   {
      OleDb::DbSettings dbSettings;
      dbSettings.Set(TEXT("openk8"),     TEXT("bmv4model"),
                     TEXT("kifDbAdmin"), TEXT("kifDbAdmin"));
      TestOleDbSelect1(dbSettings);
      TestOleDbStoredProc1(dbSettings);
      dbSettings.Free();
   }
   catch (TCHAR *exceptionMessage)
   {
      _tprintf(_T("%s\n"),exceptionMessage);
   }

   CoUninitialize();
}  // end of TestOleDbClient

// -----------------------------------------------------------------------------

void TestOledbOracleClient()
{
   CoInitialize(NULL);

   try
   {
      OleDb::DbSettings dbSettings;
      dbSettings.Set(TEXT("cafis"), TEXT("cafis"),
                     TEXT("cafis"), TEXT("cafis11"));
      dbSettings.DataSourceObject = OleDb::DbDso_Oracle;
      OleDb::DbConnection dbConnection;
      dbConnection.Connect(dbSettings);
      dbSettings.Free();
   }
   catch (TCHAR *exceptionMessage)
   {
      _tprintf(_T("%s\n"),exceptionMessage);
   }

   CoUninitialize();
}

//#define TEST_OleDbClient
#ifdef TEST_OleDbClient

void main()
{
   TestOleDbClient();
}

#endif
